package com.shop.shoporder.wapController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.PassToken;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.CartList;
import com.shop.shoporder.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/wap")
@Controller
public class CartController {
    @Autowired
    CartService cartService;

    //@UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/addCart",method = RequestMethod.POST)
    //添加购物车
    public Response addCart(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Integer bools=0;
        try{
            bools=cartService.addCart(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_ADD_FAILED,null,0,"运行异常！",request);
        }
        if(bools>0){
            return new Response(MessageConstants.MSG_SUCCESS,bools,1,"添加购物车成功！",request);
        }else{
            return new Response(MessageConstants.MSG_ADD_FAILED,null,0,"添加购物车失败！",request);
        }
    }

    //修改购物车
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/editCart",method = RequestMethod.POST)
    public Response editCart(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Boolean bools=false;
        try{
            bools=cartService.editCart(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_OPERATE_FAILED,null,0,"运行异常！",request);
        }
        if(bools!=false){
            return new Response(MessageConstants.MSG_SUCCESS,null,1,"修改购物车成功！",request);
        }else{
            return new Response(MessageConstants.MSG_EDIT_FAILED,null,0,"修改购物车失败！",request);
        }
    }

    //删除购物车
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/deleteCart",method = RequestMethod.POST)
    public Response deleteCart(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Boolean bools=false;
        try{
            bools=cartService.deleteCart(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"运行异常！",request);
        }
        if(bools!=false){
            return new Response(MessageConstants.MSG_SUCCESS,null,1,"删除购物车成功！",request);
        }else{
            return new Response(MessageConstants.MSG_DELETE_FAILED,null,0,"删除购物车失败！",request);
        }
    }

    //购物车列表
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/cartList",method = RequestMethod.POST)
    public Response cartList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        List<CartList> cartList=new ArrayList<>();
        PageInfo<CartList> cartPageInfo=new PageInfo<>(cartList);
        int pages = 0;
        int limits = 10;
        if(map.get("page")!=null){
            pages=Integer.parseInt(map.get("page"));
        }
        if(map.get("limit")!=null){
            limits=Integer.parseInt(map.get("limit"));
        }
        try{
            PageHelper.startPage(pages, limits);
            cartList=cartService.selectCartList(map,request);
            if(cartList!=null&&cartList.size()>0){
                cartPageInfo=new PageInfo<>(cartList);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
        if(cartList!=null&&cartList.size()>0){
            return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),cartPageInfo.getTotal(),"获取购物车成功！",request);
        }else{
            return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),0,"获取购物车失败！",request);
        }
    }

}
